package com.alks.function.service.impl;


import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.entity.data.entity.PcLotMtlPrice;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.mtl.GenericMaterialDocReq;
import com.alks.function.mapper.PcLotMtlPriceMapper;
import com.alks.function.service.IPcLotMtlPriceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-16
 */
@Service
public class PcLotMtlPriceServiceImpl extends ServiceImpl<PcLotMtlPriceMapper, PcLotMtlPrice> implements IPcLotMtlPriceService {
    @Autowired
    private PcLotMtlPriceMapper priceMapper;

    @Override
    @AutoPageAop
    public ResponseInfo getGenericMaterialDetail(GenericMaterialDocReq req) {
        Map<String, Object> map = new HashMap<>();
        req.setCompanyId(UserIdThread.get().getCompanyId());
        List<PcLotMtlPrice> list = priceMapper.getGenericMaterialDetail(req);
        Page<PcLotMtlPrice> page = (Page<PcLotMtlPrice>) list;
        map.put("list", list);
        map.put("total", page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo optionGenericMtl(List<PcLotMtlPrice> list, String choose) {
        for (PcLotMtlPrice price : list) {
            Integer i = priceMapper.optionGenericMtl(price, choose);
        }
        return ResponseInfo.ok();
    }

    @Override
    @Transactional
    public ResponseInfo updateGenericMtl(List<PcLotMtlPrice> list) {
        list.forEach(x->x.setSysUser(UserIdThread.get().getUserName()));
        int num = priceMapper.updateGenericMtl(list);
        return ResponseInfo.ok("修改成功");
    }

    @Override
    public ResponseInfo updateGenericMtlWP(List<PcLotMtlPrice> list, String vendorName, String vendorNo, BigDecimal price) {
        priceMapper.updateGenericMtlWP(list,vendorName,vendorNo,price, UserIdThread.get().getUserName());
        return ResponseInfo.ok("添加成功");
    }

    @Override
    @AutoPageAop
    public ResponseInfo getLotMtlUsgPrice(GenericMaterialDocReq req) {
        req.setCompanyId(UserIdThread.get().getCompanyId());
        Map<String, Object> map = new HashMap<>();
        List<PcLotMtlPrice> list = priceMapper.getLotMtlUsgPrice(req);
        Page<PcLotMtlPrice> page = (Page<PcLotMtlPrice>) list;
        map.put("list", list);
        map.put("total", page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo optionGenericLotMtl(List<PcLotMtlPrice> list, String choose) {
        for (PcLotMtlPrice price : list) {
            if (choose.equals("1")){
                priceMapper.copyLot(price);
            }else if (choose.equals("2")){
                priceMapper.deleteChooseLot(price);
            }
        }
        return ResponseInfo.ok();
    }

    @Override
    @AutoPageAop
    public ResponseInfo getLotMtlUsgPriceChe(GenericMaterialDocReq req) {
        HashMap<String, Object> map = new HashMap<>();
        req.setCompanyId(UserIdThread.get().getCompanyId());
        List<PcLotMtlPrice> list = priceMapper.getLotMtlUsgPriceChe(req);
        Page<PcLotMtlPrice> page = (Page<PcLotMtlPrice>) list;
        map.put("list", list);
        map.put("total", page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo updateLotMtlCheck(List<PcLotMtlPrice> list, String choose) {
        priceMapper.updatePcLotMtlCheck(list,choose,UserIdThread.get().getUserName());
        return ResponseInfo.ok();
    }
}
